﻿
// MY_PE_TOOLDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "MY_PE_TOOL.h"
#include "MY_PE_TOOLDlg.h"
#include "afxdialogex.h"
#include "CMyDlgComBox.h"
#include "InsertSection.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMYPETOOLDlg 对话框



CMYPETOOLDlg::CMYPETOOLDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_MY_PE_TOOL_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMYPETOOLDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_TREE1, m_TreeCtrl);
    DDX_Control(pDX, IDC_LIST1, m_ListCtrl);
    DDX_Control(pDX, EDT_VALUE, m_EditWord);
    DDX_Control(pDX, EDT_DVALUE, m_EditDword);
    DDX_Control(pDX, EDT_BVAULE, m_EditByte);
    DDX_Control(pDX, EDT_STRING_8, m_MyEditSecName);
}

BEGIN_MESSAGE_MAP(CMYPETOOLDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_SIZE()
	ON_WM_MOVE()
	ON_WM_DROPFILES()
    ON_NOTIFY(TVN_SELCHANGED, IDC_TREE1, &CMYPETOOLDlg::OnSelchangedTree1)
    ON_WM_CLOSE()
    ON_COMMAND(MN_OPEN, &CMYPETOOLDlg::OnOpen)
    ON_COMMAND(MN_SAVE, &CMYPETOOLDlg::OnSave)
    ON_COMMAND(MN_QUIT, &CMYPETOOLDlg::OnQuit)
    ON_NOTIFY(NM_CLICK, IDC_LIST1, &CMYPETOOLDlg::OnClickList1)
    ON_NOTIFY(NM_DBLCLK, IDC_LIST1, &CMYPETOOLDlg::OnDblclkList1)
    ON_EN_KILLFOCUS(EDT_VALUE, &CMYPETOOLDlg::OnKillfocusEdtValue)
    ON_EN_KILLFOCUS(EDT_DVALUE, &CMYPETOOLDlg::OnKillfocusEdtDvalue)
    ON_EN_KILLFOCUS(EDT_BVAULE, &CMYPETOOLDlg::OnKillfocusEdtBvaule)
    ON_EN_KILLFOCUS(EDT_STRING_8, &CMYPETOOLDlg::OnKillfocusEdtString8)
    ON_NOTIFY(NM_RCLICK, IDC_LIST1, &CMYPETOOLDlg::OnRclickList1)
    ON_COMMAND(MN_INCSEC, &CMYPETOOLDlg::OnIncsec)
    ON_COMMAND(MN_ADDSEC, &CMYPETOOLDlg::OnAddsec)
    ON_COMMAND(MN_ADDIMPORT, &CMYPETOOLDlg::OnAddimport)
END_MESSAGE_MAP()


// CMYPETOOLDlg 消息处理程序

BOOL CMYPETOOLDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

    //创建子窗口
    m_pDoubleListCtrl = new CDoubleList();
    m_pDoubleListCtrl->Create(IDD_DIALOG1, this);
    m_pDoubleListCtrl->SetFileData(&m_pFileData);
    m_pDoubleListCtrl->SetFilePath(m_szFileFullPath);
    m_pDoubleListCtrl->SetTreeItemText(&m_strTreeItemText);

    m_pTreeListDlg = new CTreeListDlg();
    m_pTreeListDlg->Create(IDD_DIALOG6, this);
    m_pTreeListDlg->SetFileData(&m_pFileData);

    m_pAddrRes = new CAddrRes();
    m_pAddrRes->Create(IDD_DIALOG2, this);
    m_pAddrRes->SetFileDataAddr(&m_pFileData);
    m_pAddrRes->SetFileDataSize(&m_dwFileSize);

    //隐藏Edit控件
    m_EditByte.ShowWindow(SW_HIDE);
    m_EditWord.ShowWindow(SW_HIDE);
    m_EditDword.ShowWindow(SW_HIDE);
    m_MyEditSecName.ShowWindow(SW_HIDE);

    //加载菜单
    m_Menu.LoadMenu(IDR_MENU1);
    SetMenu(&m_Menu);

    //加载树控件图标
    HICON hIcon[4];
    hIcon[0] = theApp.LoadIcon(IDI_ICON4);
    hIcon[1] = theApp.LoadIcon(IDI_ICON1);
    hIcon[2] = theApp.LoadIcon(IDI_ICON2);
    hIcon[3] = theApp.LoadIcon(IDI_ICON3);

    m_ImageList.Create(16, 16, ILC_COLOR32, 4, 4);
    for (int i = 0; i < 4; i++)
    {
        m_ImageList.Add(hIcon[i]);
    }

    //设置与树控件关联的图像列表
    m_TreeCtrl.SetImageList(&m_ImageList, TVSIL_NORMAL);

	//设置对话框标题
	SetWindowText(_T("PE_INFO"));

	//设置LIST空间风格
	m_ListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);


	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CMYPETOOLDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CMYPETOOLDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



//清空ListCtrl
void CMYPETOOLDlg::ClearListCtrl()
{
    m_ListCtrl.DeleteAllItems();
    int nColumnCount = m_ListCtrl.GetHeaderCtrl()->GetItemCount();
    for (int i = 0; i < nColumnCount; i++)
    {
        m_ListCtrl.DeleteColumn(0);
    }
}


//展开树控件
void CMYPETOOLDlg::ExpandTreeCtrl(HTREEITEM hTreeItem)
{
    if (!m_TreeCtrl.ItemHasChildren(hTreeItem))
    {
        return;
    }
    HTREEITEM hNextItem = m_TreeCtrl.GetChildItem(hTreeItem);
    while (hNextItem != NULL)
    {
        ExpandTreeCtrl(hNextItem);
        hNextItem = m_TreeCtrl.GetNextItem(hNextItem, TVGN_NEXT);
    }
    m_TreeCtrl.Expand(hTreeItem, TVE_EXPAND);
}


//打开文件初始化
void CMYPETOOLDlg::OpenFileInit()
{
    //判断拖拽的是否为文件
    BOOL bRet = PathIsDirectory(m_szFileFullPath);

    if (bRet)
    {
        MessageBox(_T("不接受拖放文件夹"), _T("提示"), MB_OK);
        return;
    }

    //获取文件名
    m_strFileName = m_szFileFullPath;
    int nIndex = m_strFileName.ReverseFind('\\');
    m_strFileName = m_strFileName.Mid(nIndex + 1);

    CString strTitle = _T("PE_INFO  [");
    strTitle += m_strFileName;
    strTitle += _T("]");

    //重新设置标题
    SetWindowText(strTitle);

    //清空ListCtrl
    ClearListCtrl();

    //清空TreeCtrl
    m_TreeCtrl.DeleteAllItems();

    CString strRootTitle = _T("文件:");
    strRootTitle += m_strFileName;
    HTREEITEM hRoot = m_TreeCtrl.InsertItem(strRootTitle, 0, 0, TVI_ROOT);

    m_TreeCtrl.InsertItem(_T("DOS头部"), 2, 2, hRoot);
    HTREEITEM hNtRoot = m_TreeCtrl.InsertItem(_T("NT头部"), 2, 2, hRoot);
    m_TreeCtrl.InsertItem(_T("文件头部"), 2, 2, hNtRoot);
    HTREEITEM hOptionRoot = m_TreeCtrl.InsertItem(_T("选项头部"), 2, 2, hNtRoot);
    m_TreeCtrl.InsertItem(_T("数据目录"), 2, 2, hOptionRoot);
    m_TreeCtrl.InsertItem(_T("节头部"), 2, 2, hRoot);



    //展开树控件
    ExpandTreeCtrl(hRoot);

    //检测是否是有效的PE文件
    m_bIsPeFile = IsPeFile(m_szFileFullPath);

    //显示文件信息
    ShowFileInfo();

    if (!m_bIsPeFile)
    {
        MessageBox(_T("不是有效的PE文件"), _T("提示"), MB_OK);
        return;
    }

    //如果有导出目录，则添加导出目录
    if (IsHaveExportTable())
    {
        m_TreeCtrl.InsertItem(_T("导出目录"), 3, 3, hRoot);
    }

    //如果有导入目录，则添加导入目录
    if (IsHaveImportTable())
    {
        m_TreeCtrl.InsertItem(_T("导入目录"), 3, 3, hRoot);
    }

    //如果有资源目录，则添加资源目录
    if (IsHaveResourceTable())
    {
        m_TreeCtrl.InsertItem(_T("资源目录"), 3, 3, hRoot);
    }

    //如果有重定位目录，则添加重定位目录
    if (IsHaveRelocationTable())
    {
        m_TreeCtrl.InsertItem(_T("重定位目录"), 3, 3, hRoot);
    }

    //如果有调试目录，则添加调试目录
    if (IsHaveDebugTable())
    {
        m_TreeCtrl.InsertItem(_T("TLS目录"), 3, 3, hRoot);
    }

    //添加地址转换
    m_TreeCtrl.InsertItem(_T("地址转换"), 1, 1, hRoot);
}


//判断是否是PE文件
BOOL CMYPETOOLDlg::IsPeFile(PTCHAR szFilePath)
{
    //打开文件
    HANDLE hFile = CreateFile(szFilePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    if (hFile == INVALID_HANDLE_VALUE)
    {
        return FALSE;
    }


    //获取文件大小
    DWORD dwFileSize = GetFileSize(hFile, NULL);

    if (dwFileSize <= 0)
    {
        CloseHandle(hFile);
        return FALSE;
    }

    m_dwFileSize = dwFileSize;

    //申请空间
    DWORD dwBytesRead = 0;
    m_pFileData = new char[dwFileSize];
    ZeroMemory(m_pFileData, dwFileSize);

    //读文件
    ReadFile(hFile, m_pFileData, dwFileSize, &dwBytesRead, NULL);

    if (dwBytesRead != dwFileSize)
    {
        delete[] m_pFileData;
        m_pFileData = NULL;
        CloseHandle(hFile);
        return FALSE;
    }

    //检查是否是PE文件
    PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)m_pFileData;
    if (pDosHeader->e_magic != 0x5A4D)
    {
        delete[] m_pFileData;
        m_pFileData = NULL;
        CloseHandle(hFile);
        return FALSE;
    }

    PIMAGE_NT_HEADERS pNtHeaders = (PIMAGE_NT_HEADERS)((DWORD)pDosHeader + pDosHeader->e_lfanew);
    if (pNtHeaders->Signature != 0x4550)
    {
        delete[] m_pFileData;
        m_pFileData = NULL;
        CloseHandle(hFile);
        return FALSE;
    }

    //关闭文件句柄
    CloseHandle(hFile);

    return TRUE;
}


//判断是否有导出表
BOOL CMYPETOOLDlg::IsHaveExportTable()
{
    PIMAGE_DOS_HEADER pDosHead = (PIMAGE_DOS_HEADER)m_pFileData;
    PIMAGE_NT_HEADERS pNtHead = (PIMAGE_NT_HEADERS)((DWORD)pDosHead + pDosHead->e_lfanew);

    if (pNtHead->OptionalHeader.DataDirectory[0].VirtualAddress != 0 && pNtHead->OptionalHeader.DataDirectory[0].Size != 0)
    {
        return TRUE;
    }

    return FALSE;
}


//判断是否有导入表
BOOL CMYPETOOLDlg::IsHaveImportTable()
{
    PIMAGE_DOS_HEADER pDosHead = (PIMAGE_DOS_HEADER)m_pFileData;
    PIMAGE_NT_HEADERS pNtHead = (PIMAGE_NT_HEADERS)((DWORD)pDosHead + pDosHead->e_lfanew);

    if (pNtHead->OptionalHeader.DataDirectory[1].VirtualAddress != 0 && pNtHead->OptionalHeader.DataDirectory[1].Size != 0)
    {
        return TRUE;
    }

    return FALSE;
}


//判断是否有资源表
BOOL CMYPETOOLDlg::IsHaveResourceTable()
{
    PIMAGE_DOS_HEADER pDosHead = (PIMAGE_DOS_HEADER)m_pFileData;
    PIMAGE_NT_HEADERS pNtHead = (PIMAGE_NT_HEADERS)((DWORD)pDosHead + pDosHead->e_lfanew);

    if (pNtHead->OptionalHeader.DataDirectory[2].VirtualAddress != 0 && pNtHead->OptionalHeader.DataDirectory[2].Size != 0)
    {
        return TRUE;
    }

    return FALSE;
}


//判断是否有重定位表
BOOL CMYPETOOLDlg::IsHaveRelocationTable()
{
    PIMAGE_DOS_HEADER pDosHead = (PIMAGE_DOS_HEADER)m_pFileData;
    PIMAGE_NT_HEADERS pNtHead = (PIMAGE_NT_HEADERS)((DWORD)pDosHead + pDosHead->e_lfanew);

    if (pNtHead->OptionalHeader.DataDirectory[5].VirtualAddress != 0 && pNtHead->OptionalHeader.DataDirectory[5].Size != 0)
    {
        return TRUE;
    }

    return FALSE;
}


//判断是否有调试表
BOOL CMYPETOOLDlg::IsHaveDebugTable()
{
    PIMAGE_DOS_HEADER pDosHead = (PIMAGE_DOS_HEADER)m_pFileData;
    PIMAGE_NT_HEADERS pNtHead = (PIMAGE_NT_HEADERS)((DWORD)pDosHead + pDosHead->e_lfanew);

    if (pNtHead->OptionalHeader.DataDirectory[9].VirtualAddress != 0 && pNtHead->OptionalHeader.DataDirectory[9].Size != 0)
    {
        return TRUE;
    }

    return FALSE;
}


//将数字转换为字符串
CString CMYPETOOLDlg::IntToString(DWORD nData, DWORD nLen)
{
    nLen *= 2;

    CString strFmt;
    strFmt.Format(_T("%%0%dX"), nLen);

    CString strRet;
    strRet.Format(strFmt, nData);

    return strRet;
}


//获取变量Size
CString CMYPETOOLDlg::GetVariableSizeToString(DWORD nLen)
{
    switch (nLen)
    {
        case sizeof(BYTE):
        {
            return _T("BYTE");
        }
        case sizeof(WORD) :
        {
            return _T("WORD");
        }
        case sizeof(DWORD) :
        {
            return _T("DWORD");
        }
    }

    return NULL;
}


//获取变量Size
DWORD CMYPETOOLDlg::GetVariableSizeToDWORD(CString strType)
{
    if (strType == "BYTE")
    {
        return 1;
    }
    
    if (strType == "WORD")
    {
        return 2;
    }

    if (strType == "DWORD")
    {
        return 4;
    }

    return 0;
}


//根据RVA计算FA
DWORD CMYPETOOLDlg::RVATOFA(DWORD dwRVA)
{
    PIMAGE_DOS_HEADER pDosHead = (PIMAGE_DOS_HEADER)(m_pFileData);
    PIMAGE_NT_HEADERS pNtHead = (PIMAGE_NT_HEADERS)((DWORD)pDosHead + pDosHead->e_lfanew);
    PIMAGE_OPTIONAL_HEADER32 pOptionalHead = &pNtHead->OptionalHeader;
    PIMAGE_SECTION_HEADER pSectionHead = (PIMAGE_SECTION_HEADER)((DWORD)&pNtHead->OptionalHeader + pNtHead->FileHeader.SizeOfOptionalHeader);

    //在节表中
    for (int i = 0; i < pNtHead->FileHeader.NumberOfSections; i++)
    {
        //判断该RVA是否在这个节区中
        if (dwRVA >= pSectionHead[i].VirtualAddress && dwRVA < pSectionHead[i].VirtualAddress + pSectionHead[i].SizeOfRawData)
        {
            return (dwRVA - pSectionHead[i].VirtualAddress + pSectionHead[i].PointerToRawData);
        }
    }

    return 0;
}


//显示文件信息
void CMYPETOOLDlg::ShowFileInfo()
{
    m_ListCtrl.ShowWindow(SW_SHOW);
    m_pDoubleListCtrl->ShowWindow(SW_HIDE);
    m_pAddrRes->ShowWindow(SW_HIDE);
    m_pTreeListDlg->ShowWindow(SW_HIDE);

    //清空列表
    ClearListCtrl();

    //添加表头
    m_ListCtrl.InsertColumn(0, _T("属性"), LVCFMT_LEFT, 120);
    m_ListCtrl.InsertColumn(1, _T("值"), LVCFMT_LEFT, 400);

    int index = 0;
    SYSTEMTIME System = { 0 };                       //获取文件的详细时间信息
    CString strFileCreateTime;                       //文件创建时间
    CString strFileWriteTime;                        //文件修改时间
    CString strFileAccessTime;                       //文件访问时间
    TCHAR szFileSize[MAX_PATH] = { 0 };              //文件大小
    WIN32_FIND_DATA WinFileData = { 0 };             //文件数据

    //查找文件，获取文件属性
    HANDLE hFile = FindFirstFile(m_szFileFullPath, &WinFileData);
    if (hFile == INVALID_HANDLE_VALUE)
    {
        return;
    }

    //将文件时间转换为系统时间格式
    FileTimeToSystemTime(&WinFileData.ftCreationTime, &System);
    strFileCreateTime.Format(_T("%04d/%02d/%02d  %02d:%02d:%02d"), 
        System.wYear, System.wMonth, System.wDay, System.wHour + 8, System.wMinute, System.wSecond);

    FileTimeToSystemTime(&WinFileData.ftLastWriteTime, &System);
    strFileWriteTime.Format(_T("%04d/%02d/%02d  %02d:%02d:%02d"),
        System.wYear, System.wMonth, System.wDay, System.wHour + 8, System.wMinute, System.wSecond);

    FileTimeToSystemTime(&WinFileData.ftLastAccessTime, &System);
    strFileAccessTime.Format(_T("%04d/%02d/%02d  %02d:%02d:%02d"),
        System.wYear, System.wMonth, System.wDay, System.wHour + 8, System.wMinute, System.wSecond);

    if (WinFileData.nFileSizeLow > 0x40000000)
    {
        _stprintf(szFileSize, TEXT("%.2lfGB"), WinFileData.nFileSizeLow / 1024.0 / 1024.0 / 1024.0);
    }   
    else if (WinFileData.nFileSizeLow > 0x100000)
    {
        _stprintf(szFileSize, TEXT("%.2lfMB (%dbytes)"), WinFileData.nFileSizeLow / 1024.0 / 1024.0, WinFileData.nFileSizeLow);
    }   
    else
    {
        _stprintf(szFileSize, TEXT("%.2lfKB (%dbytes)"), WinFileData.nFileSizeLow / 1024.0, WinFileData.nFileSizeLow);
    }

    m_ListCtrl.InsertItem(index, _T("文件名"));
    m_ListCtrl.SetItemText(index++, 1, m_strFileName);
    m_ListCtrl.InsertItem(index, _T("完整路径"));
    m_ListCtrl.SetItemText(index++, 1, m_szFileFullPath);
    m_ListCtrl.InsertItem(index, _T("文件大小"));
    m_ListCtrl.SetItemText(index++, 1, szFileSize);
    m_ListCtrl.InsertItem(index, _T("创建时间"));
    m_ListCtrl.SetItemText(index++, 1, strFileCreateTime);
    m_ListCtrl.InsertItem(index, _T("修改时间"));
    m_ListCtrl.SetItemText(index++, 1, strFileWriteTime);
    m_ListCtrl.InsertItem(index, _T("访问时间"));
    m_ListCtrl.SetItemText(index++, 1, strFileAccessTime);
}


//显示DOS头信息
void CMYPETOOLDlg::ShowDosHeadInfo()
{
    m_ListCtrl.ShowWindow(SW_SHOW);
    m_pDoubleListCtrl->ShowWindow(SW_HIDE);
    m_pAddrRes->ShowWindow(SW_HIDE);
    m_pTreeListDlg->ShowWindow(SW_HIDE);

    //清空列表
    ClearListCtrl();

    //添加列表头
    m_ListCtrl.InsertColumn(0, _T("成员"), LVCFMT_LEFT, 150);
    m_ListCtrl.InsertColumn(1, _T("偏移量"), LVCFMT_LEFT, 150);
    m_ListCtrl.InsertColumn(2, _T("大小"), LVCFMT_LEFT, 150);
    m_ListCtrl.InsertColumn(3, _T("值"), LVCFMT_LEFT, 150);

    if (!m_bIsPeFile)
    {
        MessageBox(_T("不是有效的PE文件"), _T("提示"), MB_OK);
        return;
    }

    //找到DOS头位置指针
    PIMAGE_DOS_HEADER pDosHead = (PIMAGE_DOS_HEADER)m_pFileData;

    //显示成员相关信息
    int nIndex = 0;

    m_ListCtrl.InsertItem(nIndex, _T("e_magic"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)&(pDosHead->e_magic) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_magic)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_magic, sizeof(pDosHead->e_magic)));

    m_ListCtrl.InsertItem(nIndex, _T("e_cblp"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pDosHead->e_cblp) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_cblp)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_cblp, sizeof(pDosHead->e_cblp)));

    m_ListCtrl.InsertItem(nIndex, _T("e_cp"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pDosHead->e_cp) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_cp)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_cp, sizeof(pDosHead->e_cp)));

    m_ListCtrl.InsertItem(nIndex, _T("e_crlc"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pDosHead->e_crlc) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_crlc)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_crlc, sizeof(pDosHead->e_crlc)));

    m_ListCtrl.InsertItem(nIndex, _T("e_cparhdr"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pDosHead->e_cparhdr) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_cparhdr)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_cparhdr, sizeof(pDosHead->e_cparhdr)));

    m_ListCtrl.InsertItem(nIndex, _T("e_minalloc"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pDosHead->e_minalloc) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_minalloc)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_minalloc, sizeof(pDosHead->e_minalloc)));

    m_ListCtrl.InsertItem(nIndex, _T("e_maxalloc"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pDosHead->e_maxalloc) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_maxalloc)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_maxalloc, sizeof(pDosHead->e_maxalloc)));

    m_ListCtrl.InsertItem(nIndex, _T("e_ss"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pDosHead->e_ss) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_ss)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_ss, sizeof(pDosHead->e_ss)));

    m_ListCtrl.InsertItem(nIndex, _T("e_sp"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pDosHead->e_sp) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_sp)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_sp, sizeof(pDosHead->e_sp)));

    m_ListCtrl.InsertItem(nIndex, _T("e_csum"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pDosHead->e_csum) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_csum)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_csum, sizeof(pDosHead->e_csum)));

    m_ListCtrl.InsertItem(nIndex, _T("e_ip"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pDosHead->e_ip) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_ip)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_ip, sizeof(pDosHead->e_ip)));

    m_ListCtrl.InsertItem(nIndex, _T("e_cs"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pDosHead->e_cs) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_cs)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_cs, sizeof(pDosHead->e_cs)));

    m_ListCtrl.InsertItem(nIndex, _T("e_lfarlc"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pDosHead->e_lfarlc) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_lfarlc)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_lfarlc, sizeof(pDosHead->e_lfarlc)));

    m_ListCtrl.InsertItem(nIndex, _T("e_ovno"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pDosHead->e_ovno) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_ovno)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_ovno, sizeof(pDosHead->e_ovno)));

    m_ListCtrl.InsertItem(nIndex, _T("e_res[0]"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pDosHead->e_res[0]) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_res[0])));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_res[0], sizeof(pDosHead->e_res[0])));

    m_ListCtrl.InsertItem(nIndex, _T("e_res[1]"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pDosHead->e_res[1]) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_res[1])));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_res[1], sizeof(pDosHead->e_res[1])));

    m_ListCtrl.InsertItem(nIndex, _T("e_res[2]"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pDosHead->e_res[2]) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_res[2])));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_res[2], sizeof(pDosHead->e_res[2])));

    m_ListCtrl.InsertItem(nIndex, _T("e_res[3]"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pDosHead->e_res[3]) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_res[3])));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_res[3], sizeof(pDosHead->e_res[3])));

    m_ListCtrl.InsertItem(nIndex, _T("e_oemid"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pDosHead->e_oemid) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_oemid)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_oemid, sizeof(pDosHead->e_oemid)));

    m_ListCtrl.InsertItem(nIndex, _T("e_oeminfo"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pDosHead->e_oeminfo) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_oeminfo)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_oeminfo, sizeof(pDosHead->e_oeminfo)));

    m_ListCtrl.InsertItem(nIndex, _T("e_res2[0]"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pDosHead->e_res2[0]) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_res2[0])));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_res2[0], sizeof(pDosHead->e_res2[0])));

    m_ListCtrl.InsertItem(nIndex, _T("e_res2[1]"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pDosHead->e_res2[1]) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_res2[1])));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_res2[1], sizeof(pDosHead->e_res2[1])));

    m_ListCtrl.InsertItem(nIndex, _T("e_res2[2]"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pDosHead->e_res2[2]) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_res2[2])));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_res2[2], sizeof(pDosHead->e_res2[2])));

    m_ListCtrl.InsertItem(nIndex, _T("e_res2[3]"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pDosHead->e_res2[3]) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_res2[3])));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_res2[3], sizeof(pDosHead->e_res2[3])));

    m_ListCtrl.InsertItem(nIndex, _T("e_res2[4]"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pDosHead->e_res2[4]) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_res2[4])));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_res2[4], sizeof(pDosHead->e_res2[4])));

    m_ListCtrl.InsertItem(nIndex, _T("e_res2[5]"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pDosHead->e_res2[5]) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_res2[5])));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_res2[5], sizeof(pDosHead->e_res2[5])));

    m_ListCtrl.InsertItem(nIndex, _T("e_res2[6]"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pDosHead->e_res2[6]) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_res2[6])));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_res2[6], sizeof(pDosHead->e_res2[6])));

    m_ListCtrl.InsertItem(nIndex, _T("e_res2[7]"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pDosHead->e_res2[7]) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_res2[7])));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_res2[7], sizeof(pDosHead->e_res2[7])));

    m_ListCtrl.InsertItem(nIndex, _T("e_res2[8]"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pDosHead->e_res2[8]) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_res2[8])));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_res2[8], sizeof(pDosHead->e_res2[8])));

    m_ListCtrl.InsertItem(nIndex, _T("e_res2[9]"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pDosHead->e_res2[9]) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_res2[9])));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_res2[9], sizeof(pDosHead->e_res2[9])));

    m_ListCtrl.InsertItem(nIndex, _T("e_lfanew"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pDosHead->e_lfanew) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pDosHead->e_lfanew)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pDosHead->e_lfanew, sizeof(pDosHead->e_lfanew)));
}


//显示NT头信息
void CMYPETOOLDlg::ShowNtHeadInfo()
{
    m_ListCtrl.ShowWindow(SW_SHOW);
    m_pDoubleListCtrl->ShowWindow(SW_HIDE);
    m_pAddrRes->ShowWindow(SW_HIDE);
    m_pTreeListDlg->ShowWindow(SW_HIDE);

    //清空列表
    ClearListCtrl();

    //添加列表头
    m_ListCtrl.InsertColumn(0, _T("成员"), LVCFMT_LEFT, 150);
    m_ListCtrl.InsertColumn(1, _T("偏移量"), LVCFMT_LEFT, 150);
    m_ListCtrl.InsertColumn(2, _T("大小"), LVCFMT_LEFT, 150);
    m_ListCtrl.InsertColumn(3, _T("值"), LVCFMT_LEFT, 150);

    if (!m_bIsPeFile)
    {
        MessageBox(_T("不是有效的PE文件"), _T("提示"), MB_OK);
        return;
    }

    //找到NT头位置指针
    PIMAGE_DOS_HEADER pDosHead = (PIMAGE_DOS_HEADER)m_pFileData;
    PIMAGE_NT_HEADERS pNtHead = (PIMAGE_NT_HEADERS)((DWORD)pDosHead + pDosHead->e_lfanew);

    //显示成员相关信息
    int nIndex = 0;

    m_ListCtrl.InsertItem(nIndex, _T("Signature"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pNtHead->Signature) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pNtHead->Signature)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pNtHead->Signature, sizeof(pNtHead->Signature)));
}


//显示文件头信息
void CMYPETOOLDlg::ShowFileHeadInfo()
{
    m_ListCtrl.ShowWindow(SW_SHOW);
    m_pDoubleListCtrl->ShowWindow(SW_HIDE);
    m_pAddrRes->ShowWindow(SW_HIDE);
    m_pTreeListDlg->ShowWindow(SW_HIDE);

    //清空列表
    ClearListCtrl();

    //添加列表头
    m_ListCtrl.InsertColumn(0, _T("成员"), LVCFMT_LEFT, 150);
    m_ListCtrl.InsertColumn(1, _T("偏移量"), LVCFMT_LEFT, 150);
    m_ListCtrl.InsertColumn(2, _T("大小"), LVCFMT_LEFT, 150);
    m_ListCtrl.InsertColumn(3, _T("值"), LVCFMT_LEFT, 150);

    if (!m_bIsPeFile)
    {
        MessageBox(_T("不是有效的PE文件"), _T("提示"), MB_OK);
        return;
    }

    //找到FILE头位置指针
    PIMAGE_DOS_HEADER pDosHead = (PIMAGE_DOS_HEADER)m_pFileData;
    PIMAGE_NT_HEADERS pNtHead = (PIMAGE_NT_HEADERS)((DWORD)pDosHead + pDosHead->e_lfanew);
    PIMAGE_FILE_HEADER pFileHead = &pNtHead->FileHeader;

    //显示成员相关信息
    int nIndex = 0;

    m_ListCtrl.InsertItem(nIndex, _T("Machine"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pFileHead->Machine) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pFileHead->Machine)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pFileHead->Machine, sizeof(pFileHead->Machine)));

    m_ListCtrl.InsertItem(nIndex, _T("NumberOfSections"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pFileHead->NumberOfSections) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pFileHead->NumberOfSections)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pFileHead->NumberOfSections, sizeof(pFileHead->NumberOfSections)));

    m_ListCtrl.InsertItem(nIndex, _T("TimeDateStamp"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pFileHead->TimeDateStamp) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pFileHead->TimeDateStamp)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pFileHead->TimeDateStamp, sizeof(pFileHead->TimeDateStamp)));

    m_ListCtrl.InsertItem(nIndex, _T("PointerToSymbolTable"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pFileHead->PointerToSymbolTable) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pFileHead->PointerToSymbolTable)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pFileHead->PointerToSymbolTable, sizeof(pFileHead->PointerToSymbolTable)));

    m_ListCtrl.InsertItem(nIndex, _T("NumberOfSymbols"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pFileHead->NumberOfSymbols) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pFileHead->NumberOfSymbols)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pFileHead->NumberOfSymbols, sizeof(pFileHead->NumberOfSymbols)));

    m_ListCtrl.InsertItem(nIndex, _T("SizeOfOptionalHeader"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pFileHead->SizeOfOptionalHeader) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pFileHead->SizeOfOptionalHeader)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pFileHead->SizeOfOptionalHeader, sizeof(pFileHead->SizeOfOptionalHeader)));

    m_ListCtrl.InsertItem(nIndex, _T("Characteristics"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pFileHead->Characteristics) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pFileHead->Characteristics)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pFileHead->Characteristics, sizeof(pFileHead->Characteristics)));
}


//显示选项头信息
void CMYPETOOLDlg::ShowOptionalHeadInfo()
{
    m_ListCtrl.ShowWindow(SW_SHOW);
    m_pDoubleListCtrl->ShowWindow(SW_HIDE);
    m_pAddrRes->ShowWindow(SW_HIDE);
    m_pTreeListDlg->ShowWindow(SW_HIDE);

    //清空列表
    ClearListCtrl();

    //添加列表头
    m_ListCtrl.InsertColumn(0, _T("成员"), LVCFMT_LEFT, 150);
    m_ListCtrl.InsertColumn(1, _T("偏移量"), LVCFMT_LEFT, 150);
    m_ListCtrl.InsertColumn(2, _T("大小"), LVCFMT_LEFT, 150);
    m_ListCtrl.InsertColumn(3, _T("值"), LVCFMT_LEFT, 150);

    if (!m_bIsPeFile)
    {
        MessageBox(_T("不是有效的PE文件"), _T("提示"), MB_OK);
        return;
    }

    //找到Optional头位置指针
    PIMAGE_DOS_HEADER pDosHead = (PIMAGE_DOS_HEADER)m_pFileData;
    PIMAGE_NT_HEADERS pNtHead = (PIMAGE_NT_HEADERS)((DWORD)pDosHead + pDosHead->e_lfanew);
    PIMAGE_OPTIONAL_HEADER pOptionalHead = &pNtHead->OptionalHeader;

    //显示成员相关信息
    int nIndex = 0;

    m_ListCtrl.InsertItem(nIndex, _T("Magic"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->Magic) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->Magic)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->Magic, sizeof(pOptionalHead->Magic)));

    m_ListCtrl.InsertItem(nIndex, _T("MajorLinkerVersion"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->MajorLinkerVersion) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->MajorLinkerVersion)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->MajorLinkerVersion, sizeof(pOptionalHead->MajorLinkerVersion)));

    m_ListCtrl.InsertItem(nIndex, _T("MinorLinkerVersion"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->MinorLinkerVersion) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->MinorLinkerVersion)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->MinorLinkerVersion, sizeof(pOptionalHead->MinorLinkerVersion)));

    m_ListCtrl.InsertItem(nIndex, _T("SizeOfCode"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->SizeOfCode) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->SizeOfCode)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->SizeOfCode, sizeof(pOptionalHead->SizeOfCode)));

    m_ListCtrl.InsertItem(nIndex, _T("SizeOfInitializedData"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->SizeOfInitializedData) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->SizeOfInitializedData)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->SizeOfInitializedData, sizeof(pOptionalHead->SizeOfInitializedData)));

    m_ListCtrl.InsertItem(nIndex, _T("SizeOfUninitializedData"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->SizeOfUninitializedData) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->SizeOfUninitializedData)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->SizeOfUninitializedData, sizeof(pOptionalHead->SizeOfUninitializedData)));

    m_ListCtrl.InsertItem(nIndex, _T("AddressOfEntryPoint"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->AddressOfEntryPoint) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->AddressOfEntryPoint)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->AddressOfEntryPoint, sizeof(pOptionalHead->AddressOfEntryPoint)));

    m_ListCtrl.InsertItem(nIndex, _T("BaseOfCode"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->BaseOfCode) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->BaseOfCode)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->BaseOfCode, sizeof(pOptionalHead->BaseOfCode)));

    m_ListCtrl.InsertItem(nIndex, _T("BaseOfData"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->BaseOfData) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->BaseOfData)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->BaseOfData, sizeof(pOptionalHead->BaseOfData)));

    m_ListCtrl.InsertItem(nIndex, _T("ImageBase"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->ImageBase) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->ImageBase)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->ImageBase, sizeof(pOptionalHead->ImageBase)));

    m_ListCtrl.InsertItem(nIndex, _T("SectionAlignment"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->SectionAlignment) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->SectionAlignment)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->SectionAlignment, sizeof(pOptionalHead->SectionAlignment)));

    m_ListCtrl.InsertItem(nIndex, _T("FileAlignment"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->FileAlignment) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->FileAlignment)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->FileAlignment, sizeof(pOptionalHead->FileAlignment)));

    m_ListCtrl.InsertItem(nIndex, _T("MajorOperatingSystemVersion"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->MajorOperatingSystemVersion) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->MajorOperatingSystemVersion)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->MajorOperatingSystemVersion, sizeof(pOptionalHead->MajorOperatingSystemVersion)));

    m_ListCtrl.InsertItem(nIndex, _T("MinorOperatingSystemVersion"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->MinorOperatingSystemVersion) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->MinorOperatingSystemVersion)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->MinorOperatingSystemVersion, sizeof(pOptionalHead->MinorOperatingSystemVersion)));

    m_ListCtrl.InsertItem(nIndex, _T("MajorImageVersion"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->MajorImageVersion) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->MajorImageVersion)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->MajorImageVersion, sizeof(pOptionalHead->MajorImageVersion)));

    m_ListCtrl.InsertItem(nIndex, _T("MinorImageVersion"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->MinorImageVersion) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->MinorImageVersion)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->MinorImageVersion, sizeof(pOptionalHead->MinorImageVersion)));

    m_ListCtrl.InsertItem(nIndex, _T("MajorSubsystemVersion"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->MajorSubsystemVersion) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->MajorSubsystemVersion)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->MajorSubsystemVersion, sizeof(pOptionalHead->MajorSubsystemVersion)));

    m_ListCtrl.InsertItem(nIndex, _T("MinorSubsystemVersion"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->MinorSubsystemVersion) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->MinorSubsystemVersion)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->MinorSubsystemVersion, sizeof(pOptionalHead->MinorSubsystemVersion)));

    m_ListCtrl.InsertItem(nIndex, _T("Win32VersionValue"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pOptionalHead->Win32VersionValue) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->Win32VersionValue)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->Win32VersionValue, sizeof(pOptionalHead->Win32VersionValue)));

    m_ListCtrl.InsertItem(nIndex, _T("SizeOfImage"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pOptionalHead->SizeOfImage) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->SizeOfImage)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->SizeOfImage, sizeof(pOptionalHead->SizeOfImage)));

    m_ListCtrl.InsertItem(nIndex, _T("SizeOfHeaders"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pOptionalHead->SizeOfHeaders) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->SizeOfHeaders)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->SizeOfHeaders, sizeof(pOptionalHead->SizeOfHeaders)));

    m_ListCtrl.InsertItem(nIndex, _T("CheckSum"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pOptionalHead->CheckSum) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->CheckSum)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->CheckSum, sizeof(pOptionalHead->CheckSum)));

    m_ListCtrl.InsertItem(nIndex, _T("Subsystem"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pOptionalHead->Subsystem) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->Subsystem)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->Subsystem, sizeof(pOptionalHead->Subsystem)));

    m_ListCtrl.InsertItem(nIndex, _T("DllCharacteristics"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pOptionalHead->DllCharacteristics) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DllCharacteristics)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DllCharacteristics, sizeof(pOptionalHead->DllCharacteristics)));

    m_ListCtrl.InsertItem(nIndex, _T("SizeOfStackReserve"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pOptionalHead->SizeOfStackReserve) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->SizeOfStackReserve)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->SizeOfStackReserve, sizeof(pOptionalHead->SizeOfStackReserve)));

    m_ListCtrl.InsertItem(nIndex, _T("SizeOfStackCommit"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pOptionalHead->SizeOfStackCommit) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->SizeOfStackCommit)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->SizeOfStackCommit, sizeof(pOptionalHead->SizeOfStackCommit)));

    m_ListCtrl.InsertItem(nIndex, _T("SizeOfHeapReserve"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pOptionalHead->SizeOfHeapReserve) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->SizeOfHeapReserve)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->SizeOfHeapReserve, sizeof(pOptionalHead->SizeOfHeapReserve)));

    m_ListCtrl.InsertItem(nIndex, _T("SizeOfHeapCommit"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pOptionalHead->SizeOfHeapCommit) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->SizeOfHeapCommit)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->SizeOfHeapCommit, sizeof(pOptionalHead->SizeOfHeapCommit)));

    m_ListCtrl.InsertItem(nIndex, _T("LoaderFlags"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pOptionalHead->LoaderFlags) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->LoaderFlags)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->LoaderFlags, sizeof(pOptionalHead->LoaderFlags)));

    m_ListCtrl.InsertItem(nIndex, _T("NumberOfRvaAndSizes"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pOptionalHead->NumberOfRvaAndSizes) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->NumberOfRvaAndSizes)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->NumberOfRvaAndSizes, sizeof(pOptionalHead->NumberOfRvaAndSizes)));
}


//显示数据目录
void CMYPETOOLDlg::ShowDataDirectory()
{
    m_ListCtrl.ShowWindow(SW_SHOW);
    m_pDoubleListCtrl->ShowWindow(SW_HIDE);
    m_pAddrRes->ShowWindow(SW_HIDE);
    m_pTreeListDlg->ShowWindow(SW_HIDE);

    //清空列表
    ClearListCtrl();

    //添加列表头
    m_ListCtrl.InsertColumn(0, _T("成员"), LVCFMT_LEFT, 150);
    m_ListCtrl.InsertColumn(1, _T("偏移量"), LVCFMT_LEFT, 150);
    m_ListCtrl.InsertColumn(2, _T("大小"), LVCFMT_LEFT, 150);
    m_ListCtrl.InsertColumn(3, _T("值"), LVCFMT_LEFT, 150);

    if (!m_bIsPeFile)
    {
        MessageBox(_T("不是有效的PE文件"), _T("提示"), MB_OK);
        return;
    }

    //找到NT头位置指针
    PIMAGE_DOS_HEADER pDosHead = (PIMAGE_DOS_HEADER)m_pFileData;
    PIMAGE_NT_HEADERS pNtHead = (PIMAGE_NT_HEADERS)((DWORD)pDosHead + pDosHead->e_lfanew);
    PIMAGE_OPTIONAL_HEADER pOptionalHead = &pNtHead->OptionalHeader;

    //显示成员相关信息
    int nIndex = 0;

    m_ListCtrl.InsertItem(nIndex, _T("Export Directory RVA"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->DataDirectory[0].VirtualAddress) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[0].VirtualAddress)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[0].VirtualAddress, sizeof(pOptionalHead->DataDirectory[0].VirtualAddress)));

    m_ListCtrl.InsertItem(nIndex, _T("Export Directory Size"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->DataDirectory[0].Size) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[0].Size)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[0].Size, sizeof(pOptionalHead->DataDirectory[0].Size)));

    m_ListCtrl.InsertItem(nIndex, _T("Import Directory RVA"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->DataDirectory[1].VirtualAddress) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[1].VirtualAddress)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[1].VirtualAddress, sizeof(pOptionalHead->DataDirectory[1].VirtualAddress)));

    m_ListCtrl.InsertItem(nIndex, _T("Import Directory Size"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->DataDirectory[1].Size) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[1].Size)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[1].Size, sizeof(pOptionalHead->DataDirectory[1].Size)));

    m_ListCtrl.InsertItem(nIndex, _T("Resource Directory RVA"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->DataDirectory[2].VirtualAddress) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[2].VirtualAddress)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[2].VirtualAddress, sizeof(pOptionalHead->DataDirectory[2].VirtualAddress)));

    m_ListCtrl.InsertItem(nIndex, _T("Resource Directory Size"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->DataDirectory[2].Size) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[2].Size)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[2].Size, sizeof(pOptionalHead->DataDirectory[2].Size)));

    m_ListCtrl.InsertItem(nIndex, _T("Exception Directory RVA"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->DataDirectory[3].VirtualAddress) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[3].VirtualAddress)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[3].VirtualAddress, sizeof(pOptionalHead->DataDirectory[3].VirtualAddress)));

    m_ListCtrl.InsertItem(nIndex, _T("Exception Directory Size"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->DataDirectory[3].Size) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[3].Size)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[3].Size, sizeof(pOptionalHead->DataDirectory[3].Size)));

    m_ListCtrl.InsertItem(nIndex, _T("Security Directory RVA"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->DataDirectory[4].VirtualAddress) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[4].VirtualAddress)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[4].VirtualAddress, sizeof(pOptionalHead->DataDirectory[4].VirtualAddress)));

    m_ListCtrl.InsertItem(nIndex, _T("Security Directory Size"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->DataDirectory[4].Size) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[4].Size)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[4].Size, sizeof(pOptionalHead->DataDirectory[4].Size)));

    m_ListCtrl.InsertItem(nIndex, _T("Relocation Directory RVA"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->DataDirectory[5].VirtualAddress) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[5].VirtualAddress)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[5].VirtualAddress, sizeof(pOptionalHead->DataDirectory[5].VirtualAddress)));

    m_ListCtrl.InsertItem(nIndex, _T("Relocation Directory Size"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->DataDirectory[5].Size) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[5].Size)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[5].Size, sizeof(pOptionalHead->DataDirectory[5].Size)));

    m_ListCtrl.InsertItem(nIndex, _T("Debug Directory RVA"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->DataDirectory[6].VirtualAddress) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[6].VirtualAddress)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[6].VirtualAddress, sizeof(pOptionalHead->DataDirectory[6].VirtualAddress)));

    m_ListCtrl.InsertItem(nIndex, _T("Debug Directory Size"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->DataDirectory[6].Size) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[6].Size)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[6].Size, sizeof(pOptionalHead->DataDirectory[6].Size)));

    m_ListCtrl.InsertItem(nIndex, _T("Architecture Directory RVA"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->DataDirectory[7].VirtualAddress) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[7].VirtualAddress)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[7].VirtualAddress, sizeof(pOptionalHead->DataDirectory[7].VirtualAddress)));

    m_ListCtrl.InsertItem(nIndex, _T("Architecture Directory Size"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->DataDirectory[7].Size) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[7].Size)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[7].Size, sizeof(pOptionalHead->DataDirectory[7].Size)));

    m_ListCtrl.InsertItem(nIndex, _T("Globalptr Directory RVA"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->DataDirectory[8].VirtualAddress) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[8].VirtualAddress)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[8].VirtualAddress, sizeof(pOptionalHead->DataDirectory[8].VirtualAddress)));

    m_ListCtrl.InsertItem(nIndex, _T("Globalptr Directory Size"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pOptionalHead->DataDirectory[8].Size) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[8].Size)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[8].Size, sizeof(pOptionalHead->DataDirectory[8].Size)));

    m_ListCtrl.InsertItem(nIndex, _T("TLS Directory RVA"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pOptionalHead->DataDirectory[9].VirtualAddress) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[9].VirtualAddress)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[9].VirtualAddress, sizeof(pOptionalHead->DataDirectory[9].VirtualAddress)));

    m_ListCtrl.InsertItem(nIndex, _T("TLS Directory Size"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pOptionalHead->DataDirectory[9].Size) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[9].Size)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[9].Size, sizeof(pOptionalHead->DataDirectory[9].Size)));

    m_ListCtrl.InsertItem(nIndex, _T("Configuration Directory RVA"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pOptionalHead->DataDirectory[10].VirtualAddress) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[10].VirtualAddress)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[10].VirtualAddress, sizeof(pOptionalHead->DataDirectory[10].VirtualAddress)));

    m_ListCtrl.InsertItem(nIndex, _T("Configuration Directory Size"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pOptionalHead->DataDirectory[10].Size) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[10].Size)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[10].Size, sizeof(pOptionalHead->DataDirectory[10].Size)));

    m_ListCtrl.InsertItem(nIndex, _T("Bound Import Directory RVA"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pOptionalHead->DataDirectory[11].VirtualAddress) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[11].VirtualAddress)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[11].VirtualAddress, sizeof(pOptionalHead->DataDirectory[11].VirtualAddress)));

    m_ListCtrl.InsertItem(nIndex, _T("Bound Import Directory Size"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pOptionalHead->DataDirectory[11].Size) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[11].Size)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[11].Size, sizeof(pOptionalHead->DataDirectory[11].Size)));

    m_ListCtrl.InsertItem(nIndex, _T("IAT(Import Address Table) Directory RVA"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pOptionalHead->DataDirectory[12].VirtualAddress) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[12].VirtualAddress)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[12].VirtualAddress, sizeof(pOptionalHead->DataDirectory[12].VirtualAddress)));

    m_ListCtrl.InsertItem(nIndex, _T("IAT(Import Address Table) Directory Size"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pOptionalHead->DataDirectory[12].Size) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[12].Size)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[12].Size, sizeof(pOptionalHead->DataDirectory[12].Size)));

    m_ListCtrl.InsertItem(nIndex, _T("Delay Import Directory RVA"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pOptionalHead->DataDirectory[13].VirtualAddress) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[13].VirtualAddress)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[13].VirtualAddress, sizeof(pOptionalHead->DataDirectory[13].VirtualAddress)));

    m_ListCtrl.InsertItem(nIndex, _T("Delay Import Directory Size"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pOptionalHead->DataDirectory[13].Size) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[13].Size)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[13].Size, sizeof(pOptionalHead->DataDirectory[13].Size)));

    m_ListCtrl.InsertItem(nIndex, _T("Com Descriptor Directory RVA"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pOptionalHead->DataDirectory[14].VirtualAddress) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[14].VirtualAddress)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[14].VirtualAddress, sizeof(pOptionalHead->DataDirectory[14].VirtualAddress)));

    m_ListCtrl.InsertItem(nIndex, _T("Com Descriptor Directory Size"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD)& (pOptionalHead->DataDirectory[14].Size) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pOptionalHead->DataDirectory[14].Size)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pOptionalHead->DataDirectory[14].Size, sizeof(pOptionalHead->DataDirectory[14].Size)));
}


//显示节头信息
void CMYPETOOLDlg::ShowSectionHeadInfo()
{
    m_ListCtrl.ShowWindow(SW_SHOW);
    m_pDoubleListCtrl->ShowWindow(SW_HIDE);
    m_pAddrRes->ShowWindow(SW_HIDE);
    m_pTreeListDlg->ShowWindow(SW_HIDE);

    //清空列表
    ClearListCtrl();

    //添加列表头
    m_ListCtrl.InsertColumn(0, _T("名称"), LVCFMT_LEFT, 100);
    m_ListCtrl.InsertColumn(1, _T("虚拟大小"), LVCFMT_LEFT, 100);
    m_ListCtrl.InsertColumn(2, _T("虚拟地址"), LVCFMT_LEFT, 100);
    m_ListCtrl.InsertColumn(3, _T("Raw 大小"), LVCFMT_LEFT, 100);
    m_ListCtrl.InsertColumn(4, _T("Raw 地址"), LVCFMT_LEFT, 100);
    m_ListCtrl.InsertColumn(5, _T("重定位地址"), LVCFMT_LEFT, 100);
    m_ListCtrl.InsertColumn(6, _T("Linenumbers"), LVCFMT_LEFT, 100);
    m_ListCtrl.InsertColumn(7, _T("重定位数值"), LVCFMT_LEFT, 100);
    m_ListCtrl.InsertColumn(8, _T("Linenumbers Numbers"), LVCFMT_LEFT, 100);
    m_ListCtrl.InsertColumn(9, _T("字符特征"), LVCFMT_LEFT, 100);

    if (!m_bIsPeFile)
    {
        MessageBox(_T("不是有效的PE文件"), _T("提示"), MB_OK);
        return;
    }

    //找到节表位置指针
    PIMAGE_DOS_HEADER pDosHead = (PIMAGE_DOS_HEADER)m_pFileData;
    PIMAGE_NT_HEADERS pNtHead = (PIMAGE_NT_HEADERS)((DWORD)pDosHead + pDosHead->e_lfanew);
    PIMAGE_SECTION_HEADER pSectionHead = (PIMAGE_SECTION_HEADER)((DWORD)&pNtHead->OptionalHeader + pNtHead->FileHeader.SizeOfOptionalHeader);

    //显示每个成员的大小（BYTE WORD DWORD）
    int nRow = 1;

    //将第一行空出来
    m_ListCtrl.InsertItem(0, _T("Byte[8]"));
    for (int i = 0; i < 10; i++)
    {
        BOOL n = m_ListCtrl.SetItemText(0, i, _T(" "));
    }

    m_ListCtrl.InsertItem(nRow, _T("Byte[8]"));
    m_ListCtrl.SetItemText(nRow, 1, GetVariableSizeToString(sizeof(pSectionHead->Misc.PhysicalAddress)));
    m_ListCtrl.SetItemText(nRow, 2, GetVariableSizeToString(sizeof(pSectionHead->VirtualAddress)));
    m_ListCtrl.SetItemText(nRow, 3, GetVariableSizeToString(sizeof(pSectionHead->SizeOfRawData)));
    m_ListCtrl.SetItemText(nRow, 4, GetVariableSizeToString(sizeof(pSectionHead->PointerToRawData)));
    m_ListCtrl.SetItemText(nRow, 5, GetVariableSizeToString(sizeof(pSectionHead->PointerToRelocations)));
    m_ListCtrl.SetItemText(nRow, 6, GetVariableSizeToString(sizeof(pSectionHead->PointerToLinenumbers)));
    m_ListCtrl.SetItemText(nRow, 7, GetVariableSizeToString(sizeof(pSectionHead->NumberOfRelocations)));
    m_ListCtrl.SetItemText(nRow, 8, GetVariableSizeToString(sizeof(pSectionHead->NumberOfLinenumbers)));
    m_ListCtrl.SetItemText(nRow++, 9, GetVariableSizeToString(sizeof(pSectionHead->Characteristics)));


    //显示成员相关信息
    for (int i = 0; i < pNtHead->FileHeader.NumberOfSections; i++)
    {
        char szName[9] = { 0 };
        strncpy(szName, (char *)pSectionHead[i].Name, 8);
        m_ListCtrl.InsertItem(nRow, CString(szName));
        m_ListCtrl.SetItemText(nRow, 1, IntToString(pSectionHead[i].Misc.VirtualSize, sizeof(pSectionHead[i].Misc.VirtualSize)));
        m_ListCtrl.SetItemText(nRow, 2, IntToString(pSectionHead[i].VirtualAddress, sizeof(pSectionHead[i].VirtualAddress)));
        m_ListCtrl.SetItemText(nRow, 3, IntToString(pSectionHead[i].SizeOfRawData, sizeof(pSectionHead[i].SizeOfRawData)));
        m_ListCtrl.SetItemText(nRow, 4, IntToString(pSectionHead[i].PointerToRawData, sizeof(pSectionHead[i].PointerToRawData)));
        m_ListCtrl.SetItemText(nRow, 5, IntToString(pSectionHead[i].PointerToRelocations, sizeof(pSectionHead[i].PointerToRelocations)));
        m_ListCtrl.SetItemText(nRow, 6, IntToString(pSectionHead[i].PointerToLinenumbers, sizeof(pSectionHead[i].PointerToLinenumbers)));
        m_ListCtrl.SetItemText(nRow, 7, IntToString(pSectionHead[i].NumberOfRelocations, sizeof(pSectionHead[i].NumberOfRelocations)));
        m_ListCtrl.SetItemText(nRow, 8, IntToString(pSectionHead[i].NumberOfLinenumbers, sizeof(pSectionHead[i].NumberOfLinenumbers)));
        m_ListCtrl.SetItemText(nRow++, 9, IntToString(pSectionHead[i].Characteristics, sizeof(pSectionHead[i].Characteristics)));
    }
}


//显示导出目录
void CMYPETOOLDlg::ShowExportTable()
{
    m_ListCtrl.ShowWindow(SW_HIDE);
    m_pDoubleListCtrl->ShowWindow(SW_SHOW);
    m_pAddrRes->ShowWindow(SW_HIDE);
    m_pTreeListDlg->ShowWindow(SW_HIDE);

    //显示导出目录
    m_pDoubleListCtrl->ShowExportTable();
}


//显示导入目录
void CMYPETOOLDlg::ShowImportTable()
{
    m_ListCtrl.ShowWindow(SW_HIDE);
    m_pDoubleListCtrl->ShowWindow(SW_SHOW);
    m_pAddrRes->ShowWindow(SW_HIDE);
    m_pTreeListDlg->ShowWindow(SW_HIDE);

    //显示导入目录
    m_pDoubleListCtrl->ShowImportTable();
}


//显示重定位目录
void CMYPETOOLDlg::ShowRelocationTable()
{
    m_ListCtrl.ShowWindow(SW_HIDE);
    m_pDoubleListCtrl->ShowWindow(SW_SHOW);
    m_pAddrRes->ShowWindow(SW_HIDE);
    m_pTreeListDlg->ShowWindow(SW_HIDE);

    //显示导入目录
    m_pDoubleListCtrl->ShowReloctionTable();
}


//显示TLS目录
void CMYPETOOLDlg::ShowTLSTable()
{
    m_ListCtrl.ShowWindow(SW_SHOW);
    m_pDoubleListCtrl->ShowWindow(SW_HIDE);
    m_pAddrRes->ShowWindow(SW_HIDE);
    m_pTreeListDlg->ShowWindow(SW_HIDE);

    //清空列表
    ClearListCtrl();

    //添加列表头
    m_ListCtrl.InsertColumn(0, _T("成员"), LVCFMT_LEFT, 150);
    m_ListCtrl.InsertColumn(1, _T("偏移量"), LVCFMT_LEFT, 150);
    m_ListCtrl.InsertColumn(2, _T("大小"), LVCFMT_LEFT, 150);
    m_ListCtrl.InsertColumn(3, _T("值"), LVCFMT_LEFT, 150);

    //找到NT头位置指针
    PIMAGE_DOS_HEADER pDosHead = (PIMAGE_DOS_HEADER)m_pFileData;
    PIMAGE_NT_HEADERS pNtHead = (PIMAGE_NT_HEADERS)((DWORD)pDosHead + pDosHead->e_lfanew);
    PIMAGE_OPTIONAL_HEADER pOptionalHead = &pNtHead->OptionalHeader;

    DWORD dwTlsVritualAddr = pOptionalHead->DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS].VirtualAddress;
    DWORD dwTlsSize = pOptionalHead->DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS].Size;

    PIMAGE_TLS_DIRECTORY32 pTlsDirectory = (PIMAGE_TLS_DIRECTORY32)(RVATOFA(dwTlsVritualAddr) + (DWORD)pDosHead);

    //显示成员相关信息
    int nIndex = 0;

    m_ListCtrl.InsertItem(nIndex, _T("StartAddressOfRawData"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) &(pTlsDirectory->StartAddressOfRawData) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pTlsDirectory->StartAddressOfRawData)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pTlsDirectory->StartAddressOfRawData, sizeof(pTlsDirectory->StartAddressOfRawData)));

    m_ListCtrl.InsertItem(nIndex, _T("EndAddressOfRawData"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pTlsDirectory->EndAddressOfRawData) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pTlsDirectory->EndAddressOfRawData)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pTlsDirectory->EndAddressOfRawData, sizeof(pTlsDirectory->EndAddressOfRawData)));

    m_ListCtrl.InsertItem(nIndex, _T("AddressOfIndex"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pTlsDirectory->AddressOfIndex) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pTlsDirectory->AddressOfIndex)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pTlsDirectory->AddressOfIndex, sizeof(pTlsDirectory->AddressOfIndex)));

    m_ListCtrl.InsertItem(nIndex, _T("AddressOfCallBacks"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pTlsDirectory->AddressOfCallBacks) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pTlsDirectory->AddressOfCallBacks)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pTlsDirectory->AddressOfCallBacks, sizeof(pTlsDirectory->AddressOfCallBacks)));

    m_ListCtrl.InsertItem(nIndex, _T("SizeOfZeroFill"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pTlsDirectory->SizeOfZeroFill) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pTlsDirectory->SizeOfZeroFill)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pTlsDirectory->SizeOfZeroFill, sizeof(pTlsDirectory->SizeOfZeroFill)));

    m_ListCtrl.InsertItem(nIndex, _T("Characteristics"));
    m_ListCtrl.SetItemText(nIndex, 1, IntToString((DWORD) & (pTlsDirectory->Characteristics) - (DWORD)pDosHead, 4));
    m_ListCtrl.SetItemText(nIndex, 2, GetVariableSizeToString(sizeof(pTlsDirectory->Characteristics)));
    m_ListCtrl.SetItemText(nIndex++, 3, IntToString(pTlsDirectory->Characteristics, sizeof(pTlsDirectory->Characteristics)));
}


//显示资源目录
void CMYPETOOLDlg::ShowResourceTable()
{
    m_ListCtrl.ShowWindow(SW_HIDE);
    m_pDoubleListCtrl->ShowWindow(SW_HIDE);
    m_pAddrRes->ShowWindow(SW_HIDE);
    m_pTreeListDlg->ShowWindow(SW_SHOW);

    m_pTreeListDlg->ShowResourceData();
}


//显示地址转换
void CMYPETOOLDlg::ShowAddressConvert()
{
    m_pAddrRes->ShowWindow(SW_SHOW);
    m_ListCtrl.ShowWindow(SW_HIDE);
    m_pDoubleListCtrl->ShowWindow(SW_HIDE);
    m_pTreeListDlg->ShowWindow(SW_HIDE);
}


//修改DOS头部内容
void CMYPETOOLDlg::ModifyDOSHead(LPNMITEMACTIVATE pNMItemActivate)
{
    //获得选中的行
    m_nListRow = pNMItemActivate->iItem;
    m_nListCol = 3;

    //如果没有选中，则返回
    if (m_nListRow == -1)
    {
        return;
    }

    CRect rect;

    //获取偏移
    CString strOffset = m_ListCtrl.GetItemText(m_nListRow, 1);
    //将获取的偏移字符串转化为数字
    m_dwFileOffset = wcstoul(strOffset.GetBuffer(), NULL, 16);

    //判断应该由哪个Edit控件显示，byte word 还是 dword
    if (GetVariableSizeToDWORD(m_ListCtrl.GetItemText(m_nListRow, 2)) == 1)
    {
        m_ListCtrl.GetSubItemRect(m_nListRow, m_nListCol, LVIR_LABEL, rect);//获得子项的RECT；
        m_EditByte.SetParent(&m_ListCtrl);//转换坐标为列表框中的坐标
        m_EditByte.MoveWindow(rect);//移动Edit到RECT坐在的位置;
        m_EditByte.SetWindowText(m_ListCtrl.GetItemText(m_nListRow, m_nListCol));//将该子项中的值放在Edit控件中；
        m_EditByte.ShowWindow(SW_SHOW);//显示Edit控件；
        m_EditByte.SetFocus();//设置Edit焦点
        m_EditByte.ShowCaret();//显示光标
        m_EditByte.SetSel(-1);//将光标移动到最后

        return;
    }

    if (GetVariableSizeToDWORD(m_ListCtrl.GetItemText(m_nListRow, 2)) == 2)
    {
        m_ListCtrl.GetSubItemRect(m_nListRow, m_nListCol, LVIR_LABEL, rect);//获得子项的RECT；
        m_EditWord.SetParent(&m_ListCtrl);//转换坐标为列表框中的坐标
        m_EditWord.MoveWindow(rect);//移动Edit到RECT坐在的位置;
        m_EditWord.SetWindowText(m_ListCtrl.GetItemText(m_nListRow, m_nListCol));//将该子项中的值放在Edit控件中；
        m_EditWord.ShowWindow(SW_SHOW);//显示Edit控件；
        m_EditWord.SetFocus();//设置Edit焦点
        m_EditWord.ShowCaret();//显示光标
        m_EditWord.SetSel(-1);//将光标移动到最后

        return;
    }

    if (GetVariableSizeToDWORD(m_ListCtrl.GetItemText(m_nListRow, 2)) == 4)
    {
        m_ListCtrl.GetSubItemRect(m_nListRow, m_nListCol, LVIR_LABEL, rect);//获得子项的RECT；
        m_EditDword.SetParent(&m_ListCtrl);//转换坐标为列表框中的坐标
        m_EditDword.MoveWindow(rect);//移动Edit到RECT坐在的位置;
        m_EditDword.SetWindowText(m_ListCtrl.GetItemText(m_nListRow, m_nListCol));//将该子项中的值放在Edit控件中；
        m_EditDword.ShowWindow(SW_SHOW);//显示Edit控件；
        m_EditDword.SetFocus();//设置Edit焦点
        m_EditDword.ShowCaret();//显示光标
        m_EditDword.SetSel(-1);//将光标移动到最后

        return;
    }

}


//修改NT头部内容
void CMYPETOOLDlg::ModifyNTHead(LPNMITEMACTIVATE pNMItemActivate)
{
    //获得选中的行
    m_nListRow = pNMItemActivate->iItem;
    m_nListCol = 3;

    //如果没有选中，则返回
    if (m_nListRow == -1)
    {
        return;
    }

    CRect rect;

    //获取偏移
    CString strOffset = m_ListCtrl.GetItemText(m_nListRow, 1);
    //将获取的偏移字符串转化为数字
    m_dwFileOffset = wcstoul(strOffset.GetBuffer(), NULL, 16);

    //判断应该由哪个Edit控件显示，byte word 还是 dword
    if (GetVariableSizeToDWORD(m_ListCtrl.GetItemText(m_nListRow, 2)) == 1)
    {
        m_ListCtrl.GetSubItemRect(m_nListRow, m_nListCol, LVIR_LABEL, rect);//获得子项的RECT；
        m_EditByte.SetParent(&m_ListCtrl);//转换坐标为列表框中的坐标
        m_EditByte.MoveWindow(rect);//移动Edit到RECT坐在的位置;
        m_EditByte.SetWindowText(m_ListCtrl.GetItemText(m_nListRow, m_nListCol));//将该子项中的值放在Edit控件中；
        m_EditByte.ShowWindow(SW_SHOW);//显示Edit控件；
        m_EditByte.SetFocus();//设置Edit焦点
        m_EditByte.ShowCaret();//显示光标
        m_EditByte.SetSel(-1);//将光标移动到最后

        return;
    }

    if (GetVariableSizeToDWORD(m_ListCtrl.GetItemText(m_nListRow, 2)) == 2)
    {
        m_ListCtrl.GetSubItemRect(m_nListRow, m_nListCol, LVIR_LABEL, rect);//获得子项的RECT；
        m_EditWord.SetParent(&m_ListCtrl);//转换坐标为列表框中的坐标
        m_EditWord.MoveWindow(rect);//移动Edit到RECT坐在的位置;
        m_EditWord.SetWindowText(m_ListCtrl.GetItemText(m_nListRow, m_nListCol));//将该子项中的值放在Edit控件中；
        m_EditWord.ShowWindow(SW_SHOW);//显示Edit控件；
        m_EditWord.SetFocus();//设置Edit焦点
        m_EditWord.ShowCaret();//显示光标
        m_EditWord.SetSel(-1);//将光标移动到最后

        return;
    }

    if (GetVariableSizeToDWORD(m_ListCtrl.GetItemText(m_nListRow, 2)) == 4)
    {
        m_ListCtrl.GetSubItemRect(m_nListRow, m_nListCol, LVIR_LABEL, rect);//获得子项的RECT；
        m_EditDword.SetParent(&m_ListCtrl);//转换坐标为列表框中的坐标
        m_EditDword.MoveWindow(rect);//移动Edit到RECT坐在的位置;
        m_EditDword.SetWindowText(m_ListCtrl.GetItemText(m_nListRow, m_nListCol));//将该子项中的值放在Edit控件中；
        m_EditDword.ShowWindow(SW_SHOW);//显示Edit控件；
        m_EditDword.SetFocus();//设置Edit焦点
        m_EditDword.ShowCaret();//显示光标
        m_EditDword.SetSel(-1);//将光标移动到最后

        return;
    }
}


//修改FILE头部内容
void CMYPETOOLDlg::ModifyFileHead(LPNMITEMACTIVATE pNMItemActivate)
{
    //获得选中的行
    m_nListRow = pNMItemActivate->iItem;
    m_nListCol = 3;

    //如果没有选中，则返回
    if (m_nListRow == -1)
    {
        return;
    }

    CRect rect;

    //获取偏移
    CString strOffset = m_ListCtrl.GetItemText(m_nListRow, 1);
    //将获取的偏移字符串转化为数字
    m_dwFileOffset = wcstoul(strOffset.GetBuffer(), NULL, 16);

    //如果选中的是Machine
    if (m_nListRow == 0)
    {
        ModifyFileHeadMachine(m_nListRow, m_nListCol, m_dwFileOffset);
        return;
    }

    //判断应该由哪个Edit控件显示，byte word 还是 dword
    if (GetVariableSizeToDWORD(m_ListCtrl.GetItemText(m_nListRow, 2)) == 1)
    {
        m_ListCtrl.GetSubItemRect(m_nListRow, m_nListCol, LVIR_LABEL, rect);//获得子项的RECT；
        m_EditByte.SetParent(&m_ListCtrl);//转换坐标为列表框中的坐标
        m_EditByte.MoveWindow(rect);//移动Edit到RECT坐在的位置;
        m_EditByte.SetWindowText(m_ListCtrl.GetItemText(m_nListRow, m_nListCol));//将该子项中的值放在Edit控件中；
        m_EditByte.ShowWindow(SW_SHOW);//显示Edit控件；
        m_EditByte.SetFocus();//设置Edit焦点
        m_EditByte.ShowCaret();//显示光标
        m_EditByte.SetSel(-1);//将光标移动到最后

        return;
    }

    if (GetVariableSizeToDWORD(m_ListCtrl.GetItemText(m_nListRow, 2)) == 2)
    {
        m_ListCtrl.GetSubItemRect(m_nListRow, m_nListCol, LVIR_LABEL, rect);//获得子项的RECT；
        m_EditWord.SetParent(&m_ListCtrl);//转换坐标为列表框中的坐标
        m_EditWord.MoveWindow(rect);//移动Edit到RECT坐在的位置;
        m_EditWord.SetWindowText(m_ListCtrl.GetItemText(m_nListRow, m_nListCol));//将该子项中的值放在Edit控件中；
        m_EditWord.ShowWindow(SW_SHOW);//显示Edit控件；
        m_EditWord.SetFocus();//设置Edit焦点
        m_EditWord.ShowCaret();//显示光标
        m_EditWord.SetSel(-1);//将光标移动到最后

        return;
    }

    if (GetVariableSizeToDWORD(m_ListCtrl.GetItemText(m_nListRow, 2)) == 4)
    {
        m_ListCtrl.GetSubItemRect(m_nListRow, m_nListCol, LVIR_LABEL, rect);//获得子项的RECT；
        m_EditDword.SetParent(&m_ListCtrl);//转换坐标为列表框中的坐标
        m_EditDword.MoveWindow(rect);//移动Edit到RECT坐在的位置;
        m_EditDword.SetWindowText(m_ListCtrl.GetItemText(m_nListRow, m_nListCol));//将该子项中的值放在Edit控件中；
        m_EditDword.ShowWindow(SW_SHOW);//显示Edit控件；
        m_EditDword.SetFocus();//设置Edit焦点
        m_EditDword.ShowCaret();//显示光标
        m_EditDword.SetSel(-1);//将光标移动到最后

        return;
    }
}


void CMYPETOOLDlg::ModifyFileHeadMachine(int nRow, int nCol, DWORD dwFileOffset)
{
    //获取值和偏移
    CString strValue = m_ListCtrl.GetItemText(m_nListRow, m_nListCol);

    //将输入的值转为数字
    WORD dwValue = wcstoul(strValue.GetBuffer(), NULL, 16);

    CMyDlgComBox DlgCombox;
    DlgCombox.SetValue(&dwValue);
    DlgCombox.SetFlag(FILE_MACHINE);
    DlgCombox.SetFilePath(m_szFileFullPath);
    DlgCombox.SetFileOffset(dwFileOffset);
    DlgCombox.DoModal();

    //将新值显示
    m_ListCtrl.SetItemText(nRow, nCol, IntToString(dwValue, sizeof(dwValue)));
}


//修改OPTIONAL头部内容
void CMYPETOOLDlg::ModifyOptionalHead(LPNMITEMACTIVATE pNMItemActivate)
{
    //获得选中的行
    m_nListRow = pNMItemActivate->iItem;
    m_nListCol = 3;

    //如果没有选中，则返回
    if (m_nListRow == -1)
    {
        return;
    }

    CRect rect;

    //获取偏移
    CString strOffset = m_ListCtrl.GetItemText(m_nListRow, 1);
    //将获取的偏移字符串转化为数字
    m_dwFileOffset = wcstoul(strOffset.GetBuffer(), NULL, 16);

    //判断应该由哪个Edit控件显示，byte word 还是 dword
    if (GetVariableSizeToDWORD(m_ListCtrl.GetItemText(m_nListRow, 2)) == 1)
    {
        m_ListCtrl.GetSubItemRect(m_nListRow, m_nListCol, LVIR_LABEL, rect);//获得子项的RECT；
        m_EditByte.SetParent(&m_ListCtrl);//转换坐标为列表框中的坐标
        m_EditByte.MoveWindow(rect);//移动Edit到RECT坐在的位置;
        m_EditByte.SetWindowText(m_ListCtrl.GetItemText(m_nListRow, m_nListCol));//将该子项中的值放在Edit控件中；
        m_EditByte.ShowWindow(SW_SHOW);//显示Edit控件；
        m_EditByte.SetFocus();//设置Edit焦点
        m_EditByte.ShowCaret();//显示光标
        m_EditByte.SetSel(-1);//将光标移动到最后

        return;
    }

    if (GetVariableSizeToDWORD(m_ListCtrl.GetItemText(m_nListRow, 2)) == 2)
    {
        m_ListCtrl.GetSubItemRect(m_nListRow, m_nListCol, LVIR_LABEL, rect);//获得子项的RECT；
        m_EditWord.SetParent(&m_ListCtrl);//转换坐标为列表框中的坐标
        m_EditWord.MoveWindow(rect);//移动Edit到RECT坐在的位置;
        m_EditWord.SetWindowText(m_ListCtrl.GetItemText(m_nListRow, m_nListCol));//将该子项中的值放在Edit控件中；
        m_EditWord.ShowWindow(SW_SHOW);//显示Edit控件；
        m_EditWord.SetFocus();//设置Edit焦点
        m_EditWord.ShowCaret();//显示光标
        m_EditWord.SetSel(-1);//将光标移动到最后

        return;
    }

    if (GetVariableSizeToDWORD(m_ListCtrl.GetItemText(m_nListRow, 2)) == 4)
    {
        m_ListCtrl.GetSubItemRect(m_nListRow, m_nListCol, LVIR_LABEL, rect);//获得子项的RECT；
        m_EditDword.SetParent(&m_ListCtrl);//转换坐标为列表框中的坐标
        m_EditDword.MoveWindow(rect);//移动Edit到RECT坐在的位置;
        m_EditDword.SetWindowText(m_ListCtrl.GetItemText(m_nListRow, m_nListCol));//将该子项中的值放在Edit控件中；
        m_EditDword.ShowWindow(SW_SHOW);//显示Edit控件；
        m_EditDword.SetFocus();//设置Edit焦点
        m_EditDword.ShowCaret();//显示光标
        m_EditDword.SetSel(-1);//将光标移动到最后

        return;
    }
}


//修改节头部内容
void CMYPETOOLDlg::ModifySectionHead(LPNMITEMACTIVATE pNMItemActivate)
{
    //获得选中的行
    m_nListRow = pNMItemActivate->iItem;
    m_nListCol = pNMItemActivate->iSubItem;

    //如果选中第一行和第2行或者没选中 则返回
    if (m_nListRow <= 1)
    {
        return;
    }

    CRect rect;

    //获取偏移
    CString strOffset = m_ListCtrl.GetItemText(0, m_nListCol);
    //将获取的偏移字符串转化为数字
    m_dwFileOffset = wcstoul(strOffset.GetBuffer(), NULL, 16);

    //如果选中的是节表的名字
    if (m_nListCol == 0)
    {
        m_ListCtrl.GetSubItemRect(m_nListRow, m_nListCol, LVIR_LABEL, rect);//获得子项的RECT；
        m_MyEditSecName.SetParent(&m_ListCtrl);//转换坐标为列表框中的坐标
        m_MyEditSecName.MoveWindow(rect);//移动Edit到RECT坐在的位置;
        m_MyEditSecName.SetWindowText(m_ListCtrl.GetItemText(m_nListRow, m_nListCol));//将该子项中的值放在Edit控件中；
        m_MyEditSecName.ShowWindow(SW_SHOW);//显示Edit控件；
        m_MyEditSecName.SetFocus();//设置Edit焦点
        m_MyEditSecName.ShowCaret();//显示光标
        m_MyEditSecName.SetSel(-1);//将光标移动到最后

        return;
    }
    else
    {
        //判断应该由哪个Edit控件显示，byte word 还是 dword
        if (GetVariableSizeToDWORD(m_ListCtrl.GetItemText(1, m_nListCol)) == 1)
        {
            m_ListCtrl.GetSubItemRect(m_nListRow, m_nListCol, LVIR_LABEL, rect);//获得子项的RECT；
            m_EditByte.SetParent(&m_ListCtrl);//转换坐标为列表框中的坐标
            m_EditByte.MoveWindow(rect);//移动Edit到RECT坐在的位置;
            m_EditByte.SetWindowText(m_ListCtrl.GetItemText(m_nListRow, m_nListCol));//将该子项中的值放在Edit控件中；
            m_EditByte.ShowWindow(SW_SHOW);//显示Edit控件；
            m_EditByte.SetFocus();//设置Edit焦点
            m_EditByte.ShowCaret();//显示光标
            m_EditByte.SetSel(-1);//将光标移动到最后

            return;
        }

        if (GetVariableSizeToDWORD(m_ListCtrl.GetItemText(1, m_nListCol)) == 2)
        {
            m_ListCtrl.GetSubItemRect(m_nListRow, m_nListCol, LVIR_LABEL, rect);//获得子项的RECT；
            m_EditWord.SetParent(&m_ListCtrl);//转换坐标为列表框中的坐标
            m_EditWord.MoveWindow(rect);//移动Edit到RECT坐在的位置;
            m_EditWord.SetWindowText(m_ListCtrl.GetItemText(m_nListRow, m_nListCol));//将该子项中的值放在Edit控件中；
            m_EditWord.ShowWindow(SW_SHOW);//显示Edit控件；
            m_EditWord.SetFocus();//设置Edit焦点
            m_EditWord.ShowCaret();//显示光标
            m_EditWord.SetSel(-1);//将光标移动到最后

            return;
        }

        if (GetVariableSizeToDWORD(m_ListCtrl.GetItemText(1, m_nListCol)) == 4)
        {
            m_ListCtrl.GetSubItemRect(m_nListRow, m_nListCol, LVIR_LABEL, rect);//获得子项的RECT；
            m_EditDword.SetParent(&m_ListCtrl);//转换坐标为列表框中的坐标
            m_EditDword.MoveWindow(rect);//移动Edit到RECT坐在的位置;
            m_EditDword.SetWindowText(m_ListCtrl.GetItemText(m_nListRow, m_nListCol));//将该子项中的值放在Edit控件中；
            m_EditDword.ShowWindow(SW_SHOW);//显示Edit控件；
            m_EditDword.SetFocus();//设置Edit焦点
            m_EditDword.ShowCaret();//显示光标
            m_EditDword.SetSel(-1);//将光标移动到最后

            return;
        }
    }
}


//WriteFile
BOOL CMYPETOOLDlg::MyWriteFile(DWORD dwOffset, DWORD dwSize, void* pValue)
{
    //打开文件
    HANDLE hFile = CreateFile(m_szFileFullPath, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    if (hFile == INVALID_HANDLE_VALUE)
    {
        MessageBox(_T("打开文件失败"), _T("提示"), MB_OK);
        return FALSE;
    }

    //设置文件指针偏移
    DWORD D = SetFilePointer(hFile, dwOffset, NULL, FILE_BEGIN);

    //写文件
    DWORD dwWriteBytes = 0;
    BOOL bRet = WriteFile(hFile, pValue, dwSize, &dwWriteBytes, NULL);
    if (bRet == FALSE)
    {
        MessageBox(_T("写入文件失败"), _T("提示"), MB_OK);
        CloseHandle(hFile);
        return FALSE;
    }

    //关闭句柄
    CloseHandle(hFile);
    return TRUE;
}


//WriteBuff
BOOL CMYPETOOLDlg::MyWriteBuff(DWORD dwOffset, DWORD dwSize, void* pValue)
{
    char* pBuffPoint = NULL;

    if (m_pFileData != NULL)
    {
        pBuffPoint = m_pFileData + dwOffset;

        switch (dwSize)
        {
        case 1:
            *pBuffPoint = *(char *)pValue;
            break;
        case 2:
            *(WORD *)pBuffPoint = *(WORD *)pValue;
            break;
        case 4:
            *(DWORD*)pBuffPoint = *(DWORD *)pValue;
            break;
        default:
        {
            for (int i = 0; i < dwSize; i++)
            {
                pBuffPoint[i] = ((char *)pValue)[i];
            }
        }

        }
    }

    return TRUE;
}


void CMYPETOOLDlg::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);
    CRect rectDlg;

    if (m_TreeCtrl)
    {
        GetClientRect(rectDlg);
        rectDlg.DeflateRect(0, 0, 0, 0);
        rectDlg.right = 300;
        m_TreeCtrl.MoveWindow(rectDlg);
    }
    if (m_ListCtrl)
    {
        GetClientRect(rectDlg);
        rectDlg.DeflateRect(305, 0, 0, 0);
        m_ListCtrl.MoveWindow(rectDlg);
    }
    if (m_pDoubleListCtrl)
    {    
        GetClientRect(rectDlg);
        rectDlg.DeflateRect(305, 0, 0, 0);
        m_pDoubleListCtrl->MoveWindow(rectDlg);
    }
    if (m_pAddrRes)
    {
        GetClientRect(rectDlg);
        rectDlg.DeflateRect(305, 0, 0, 0);
        m_pAddrRes->MoveWindow(rectDlg);
    }
    if (m_pTreeListDlg)
    {
        GetClientRect(rectDlg);
        rectDlg.DeflateRect(305, 0, 0, 0);
        m_pTreeListDlg->MoveWindow(rectDlg);
    }
}


void CMYPETOOLDlg::OnMove(int x, int y)
{
	CDialogEx::OnMove(x, y);

    CRect rectDlg;

    if (m_ListCtrl)
    {
        GetClientRect(rectDlg);
        rectDlg.DeflateRect(305, 0, 0, 0);
        m_ListCtrl.MoveWindow(rectDlg);
    }
    if (m_TreeCtrl)
    {
        GetClientRect(rectDlg);
        rectDlg.DeflateRect(0, 0, 0, 0);
        rectDlg.right = 300;
        m_TreeCtrl.MoveWindow(rectDlg);
    }
    if (m_pDoubleListCtrl)
    {
        GetClientRect(rectDlg);
        rectDlg.DeflateRect(305, 0, 0, 0);
        m_pDoubleListCtrl->MoveWindow(rectDlg);
    }
    if (m_pAddrRes)
    {
        GetClientRect(rectDlg);
        rectDlg.DeflateRect(305, 0, 0, 0);
        m_pDoubleListCtrl->MoveWindow(rectDlg);
    }
    if (m_pTreeListDlg)
    {
        GetClientRect(rectDlg);
        rectDlg.DeflateRect(305, 0, 0, 0);
        m_pTreeListDlg->MoveWindow(rectDlg);
    }
}


void CMYPETOOLDlg::OnDropFiles(HDROP hDropInfo)
{
    //将原先的数据释放
    if (m_pFileData)
    {
        delete[] m_pFileData;
        m_pFileData = NULL;
    }

    //获取拖拽文件的个数
    int DropCount = DragQueryFile(hDropInfo, -1, NULL, 0);

    if (DropCount != 1)
    {
        MessageBox(_T("只允许载入一个文件, 不允许载入多个文件"), _T("提示"), MB_OK);
        return;
    }

    ZeroMemory(m_szFileFullPath, MAX_PATH * sizeof(TCHAR));

    //获取拖拽文件的全路径
    DragQueryFile(hDropInfo, 0, m_szFileFullPath, MAX_PATH);

    //释放为拖拽功能而分配的内存
    DragFinish(hDropInfo);

    CDialogEx::OnDropFiles(hDropInfo);

    OpenFileInit();
}


void CMYPETOOLDlg::OnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult)
{
    LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);

    //获取树控件选中的字符串
    m_strTreeItemText = m_TreeCtrl.GetItemText(pNMTreeView->itemNew.hItem);

    CString strRootName = _T("文件:") + m_strFileName;

    if (m_strTreeItemText == strRootName)
    {
        ShowFileInfo();
    }
    else if (m_strTreeItemText == _T("DOS头部"))
    {
        ShowDosHeadInfo();
    }
    else if (m_strTreeItemText == _T("NT头部"))
    {
        ShowNtHeadInfo();
    }
    else if (m_strTreeItemText == _T("文件头部"))
    {
        ShowFileHeadInfo();
    }
    else if (m_strTreeItemText == _T("选项头部"))
    {
        ShowOptionalHeadInfo();
    }
    else if (m_strTreeItemText == _T("数据目录"))
    {
        ShowDataDirectory();
    }
    else if (m_strTreeItemText == _T("节头部"))
    {
        ShowSectionHeadInfo();
    }
    else if (m_strTreeItemText == _T("导出目录"))
    {
        ShowExportTable();
    }
    else if (m_strTreeItemText == _T("导入目录"))
    {
        ShowImportTable();
    }
    else if (m_strTreeItemText == _T("重定位目录"))
    {
        ShowRelocationTable();
    }
    else if (m_strTreeItemText == _T("TLS目录"))
    {
        ShowTLSTable();
    }
    else if (m_strTreeItemText == _T("资源目录"))
    {
        ShowResourceTable();
    }
    else if (m_strTreeItemText == _T("Hex编辑器"))
    {
        //PrintHexEditor();
    }
    else if (m_strTreeItemText == _T("地址转换"))
    {
        ShowAddressConvert();
    }
    else if (m_strTreeItemText == _T("快速反汇编"))
    {
        //PrintDisasmDlg();
    }

    *pResult = 0;
}


void CMYPETOOLDlg::OnClose()
{
    if (m_pFileData != NULL)
    {
        delete[] m_pFileData;
        m_pFileData = NULL;
    }

    if (m_pAddrRes != NULL)
    {
        delete m_pAddrRes;
        m_pAddrRes = NULL;
    }

    if (m_pDoubleListCtrl != NULL)
    {
        delete m_pDoubleListCtrl;
        m_pDoubleListCtrl = NULL;
    }




CDialogEx::OnClose();
}


//菜单：打开
void CMYPETOOLDlg::OnOpen()
{
    //将原先的数据释放
    if (m_pFileData)
    {
        delete[] m_pFileData;
        m_pFileData = NULL;
    }

    ZeroMemory(m_szFileFullPath, MAX_PATH * sizeof(TCHAR));

    CString strFileName;

    CFileDialog FileDlg(TRUE, _T("全部文件(*.*)|*.*|可执行文件(*.exe,*.dll)|*.exe;*.dll"),
        NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
        _T("全部文件(*.*)|*.*|可执行文件(*.exe,*.dll)|*.exe;*.dll||"), this);

    FileDlg.DoModal();

    strFileName = FileDlg.GetPathName();
    lstrcpy(m_szFileFullPath, strFileName.GetBuffer());

    OpenFileInit();
}


//菜单：保存
void CMYPETOOLDlg::OnSave()
{

}


//菜单：退出
void CMYPETOOLDlg::OnQuit()
{
    if (m_pFileData != NULL)
    {
        delete[] m_pFileData;
        m_pFileData = NULL;
    }

    if (m_pAddrRes != NULL)
    {
        delete m_pAddrRes;
        m_pAddrRes = NULL;
    }

    if (m_pDoubleListCtrl != NULL)
    {
        delete m_pDoubleListCtrl;
        m_pDoubleListCtrl = NULL;
    }

    SendMessage(WM_CLOSE);
}


//ListCtrl_1  左键单击
void CMYPETOOLDlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
    LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
    *pResult = 0;

    CString strFmt;

    //如果ListCtrl是节头部，则点击相应节表显示对应偏移
    if (m_strTreeItemText == _T("节头部"))
    {
        for (int i = 0; i < 10; i++)
        {
            BOOL n = m_ListCtrl.SetItemText(0, i, _T(" "));
        }

        POSITION pos = m_ListCtrl.GetFirstSelectedItemPosition();
        int	nIdx = m_ListCtrl.GetNextSelectedItem(pos);

        if (nIdx < 2)
        {
            return;
        }

        //找到节表位置指针
        PIMAGE_DOS_HEADER pDosHead = (PIMAGE_DOS_HEADER)m_pFileData;
        PIMAGE_NT_HEADERS pNtHead = (PIMAGE_NT_HEADERS)((DWORD)pDosHead + pDosHead->e_lfanew);
        PIMAGE_SECTION_HEADER pSectionHead = (PIMAGE_SECTION_HEADER)((DWORD)&pNtHead->OptionalHeader + pNtHead->FileHeader.SizeOfOptionalHeader);

        //找到对应的节名称
        for (int i = 0; i < pNtHead->FileHeader.NumberOfSections; i++)
        {
            //根据选中的行数确定选中哪个节
            if (pNMItemActivate->iItem - 2 == i)
            {
                m_ListCtrl.SetItemText(0, 0, IntToString((DWORD)(pSectionHead[i].Name) - (DWORD)pDosHead, 4));
                m_ListCtrl.SetItemText(0, 1, IntToString((DWORD) & (pSectionHead[i].Misc.VirtualSize) - (DWORD)pDosHead, 4));
                m_ListCtrl.SetItemText(0, 2, IntToString((DWORD) & (pSectionHead[i].VirtualAddress) - (DWORD)pDosHead, 4));
                m_ListCtrl.SetItemText(0, 3, IntToString((DWORD) & (pSectionHead[i].SizeOfRawData) - (DWORD)pDosHead, 4));
                m_ListCtrl.SetItemText(0, 4, IntToString((DWORD) & (pSectionHead[i].PointerToRawData) - (DWORD)pDosHead, 4));
                m_ListCtrl.SetItemText(0, 5, IntToString((DWORD) & (pSectionHead[i].PointerToRelocations) - (DWORD)pDosHead, 4));
                m_ListCtrl.SetItemText(0, 6, IntToString((DWORD) & (pSectionHead[i].PointerToLinenumbers) - (DWORD)pDosHead, 4));
                m_ListCtrl.SetItemText(0, 7, IntToString((DWORD) & (pSectionHead[i].NumberOfRelocations) - (DWORD)pDosHead, 4));
                m_ListCtrl.SetItemText(0, 8, IntToString((DWORD) & (pSectionHead[i].NumberOfLinenumbers) - (DWORD)pDosHead, 4));
                m_ListCtrl.SetItemText(0, 9, IntToString((DWORD) & (pSectionHead[i].Characteristics) - (DWORD)pDosHead, 4));

                return;
            }
        }
    }
}


//键盘消息
BOOL CMYPETOOLDlg::PreTranslateMessage(MSG* pMsg)
{
    if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN)
    {
        if (pMsg->hwnd == m_EditWord.GetSafeHwnd())
        {
            //将EditCtrl的焦点结束
            m_EditWord.PostMessage(WM_KILLFOCUS, 0, 0);
            return TRUE;
        }
        else if (pMsg->hwnd == m_EditDword.GetSafeHwnd())
        {
            //将EditCtrl的焦点结束
            m_EditDword.PostMessage(WM_KILLFOCUS, 0, 0);
            return TRUE;
        }
        else if (pMsg->hwnd == m_EditByte.GetSafeHwnd())
        {
            //将EditCtrl的焦点结束
            m_EditByte.PostMessage(WM_KILLFOCUS, 0, 0);
            return TRUE;
        }
        else if (pMsg->hwnd == m_MyEditSecName.GetSafeHwnd())
        {
            //将EditCtrl的焦点结束
            m_MyEditSecName.PostMessage(WM_KILLFOCUS, 0, 0);
            return TRUE;
        }
    }

    return CDialogEx::PreTranslateMessage(pMsg);
}


//ListCtrl 左键双击
void CMYPETOOLDlg::OnDblclkList1(NMHDR* pNMHDR, LRESULT* pResult)
{
    LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);

    if (m_strTreeItemText == _T("DOS头部"))
    {
        ModifyDOSHead(pNMItemActivate);
    }
    else if (m_strTreeItemText == _T("NT头部"))
    {
        ModifyNTHead(pNMItemActivate);
    }
    else if (m_strTreeItemText == _T("文件头部"))
    {
        ModifyFileHead(pNMItemActivate);
    }
    else if (m_strTreeItemText == _T("选项头部"))
    {
        ModifyOptionalHead(pNMItemActivate);
    }
    else if(m_strTreeItemText == _T("节头部"))
    {
        ModifySectionHead(pNMItemActivate);
    }
    else
    {

    }

    *pResult = 0;
}


//m_EditWord 失去控件焦点消息
void CMYPETOOLDlg::OnKillfocusEdtValue()
{
    CString strText;

    //得到用户输入的新的内容
    m_EditWord.GetWindowText(strText);

    //设置编辑框的新内容
    if (!strText.IsEmpty())
    {
        m_ListCtrl.SetItemText(m_nListRow, m_nListCol, strText);
    }
    else
    {
        //隐藏编辑框
        m_EditWord.ShowWindow(SW_HIDE);
        return;
    }

    //将编辑框中的内容置空
    m_EditWord.SetWindowText(NULL);

    //将输入的值转为数字
    DWORD dwValue = wcstoul(strText.GetBuffer(), NULL, 16);

    //将值写入文件
    MyWriteFile(m_dwFileOffset, 2, &dwValue);

    //修改缓冲区中的内容
    MyWriteBuff(m_dwFileOffset, 2, &dwValue);

    //隐藏编辑框
    m_EditWord.ShowWindow(SW_HIDE);
}



//m_EditDword 失去控件焦点消息
void CMYPETOOLDlg::OnKillfocusEdtDvalue()
{
    CString strText;

    //得到用户输入的新的内容
    m_EditDword.GetWindowText(strText);

    //设置编辑框的新内容
    if (!strText.IsEmpty())
    {
        m_ListCtrl.SetItemText(m_nListRow, m_nListCol, strText);
    }
    else
    {
        //隐藏编辑框
        m_EditDword.ShowWindow(SW_HIDE);
        return;
    }

    //将编辑框中的内容置空
    m_EditDword.SetWindowText(NULL);

    //将输入的值转为数字
    DWORD dwValue = wcstoul(strText.GetBuffer(), NULL, 16);

    //将值写入文件
    MyWriteFile(m_dwFileOffset, 4, &dwValue);

    //修改缓冲区中的内容
    MyWriteBuff(m_dwFileOffset, 4, &dwValue);

    //隐藏编辑框
    m_EditDword.ShowWindow(SW_HIDE);
}


//m_EditByte 失去控件焦点消息
void CMYPETOOLDlg::OnKillfocusEdtBvaule()
{
    CString strText;

    //得到用户输入的新的内容
    m_EditByte.GetWindowText(strText);

    //设置编辑框的新内容
    if (!strText.IsEmpty())
    {
        m_ListCtrl.SetItemText(m_nListRow, m_nListCol, strText);
    }
    else
    {
        //隐藏编辑框
        m_EditByte.ShowWindow(SW_HIDE);
        return;
    }

    //将编辑框中的内容置空
    m_EditByte.SetWindowText(NULL);

    //将输入的值转为数字
    DWORD dwValue = wcstoul(strText.GetBuffer(), NULL, 16);

    //将值写入文件
    MyWriteFile(m_dwFileOffset, 1, &dwValue);

    //修改缓冲区中的内容
    MyWriteBuff(m_dwFileOffset, 1, &dwValue);

    //隐藏编辑框
    m_EditByte.ShowWindow(SW_HIDE);
}


//m_MyEditSecName 失去控件焦点消息
void CMYPETOOLDlg::OnKillfocusEdtString8()
{
    CString strText;
    char szBuff[8] = { 0 };

    //得到用户输入的新的内容
    m_MyEditSecName.GetWindowText(strText);

    WideCharToMultiByte(CP_ACP, 0, strText, -1, szBuff, 8, NULL, NULL);


    //设置编辑框的新内容
    if (!strText.IsEmpty())
    {
        m_ListCtrl.SetItemText(m_nListRow, m_nListCol, strText);
    }
    else
    {
        //隐藏编辑框
        m_MyEditSecName.ShowWindow(SW_HIDE);
        return;
    }

    //将编辑框中的内容置空
    m_MyEditSecName.SetWindowText(NULL);

    //将值写入文件
    MyWriteFile(m_dwFileOffset, 8, szBuff);

    //修改缓冲区中的内容
    MyWriteBuff(m_dwFileOffset, 8, szBuff);

    //隐藏编辑框
    m_MyEditSecName.ShowWindow(SW_HIDE);
}


//ListCtrl 右键单击
void CMYPETOOLDlg::OnRclickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
    LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
    
    //设置鼠标的坐标是相对于ListCtrl的
    m_ListCtrl.ClientToScreen(&pNMItemActivate->ptAction);

    CMenu menu;

    if (m_strTreeItemText == _T("节头部"))
    {
        //从资源加载菜单
        menu.LoadMenu(IDR_MENU2);

        //获取子菜单
        CMenu* pSubMenu = menu.GetSubMenu(0);

        //弹出子菜单
        pSubMenu->TrackPopupMenu(
            TPM_LEFTALIGN,
            pNMItemActivate->ptAction.x,
            pNMItemActivate->ptAction.y,
            this);
    }


    *pResult = 0;
}


//扩大最后一个节
void CMYPETOOLDlg::OnIncsec()
{
    InsertSection InsertSec;
    InsertSec.SetSectionType(INC_SECTION);
    InsertSec.SetFilePath(m_szFileFullPath);
    InsertSec.SetFileData(&m_pFileData);
    InsertSec.DoModal();
}


//增加一个节
void CMYPETOOLDlg::OnAddsec()
{
    //最少空余的空间应该有一个节表的大小
    DWORD dwSectionHeadSize = sizeof(IMAGE_SECTION_HEADER);

    //计算到节表头结束占用了多少空间
    PIMAGE_DOS_HEADER pDosHead = (PIMAGE_DOS_HEADER)m_pFileData;
    PIMAGE_NT_HEADERS pNtHead = (PIMAGE_NT_HEADERS)((DWORD)pDosHead + pDosHead->e_lfanew);
    PIMAGE_SECTION_HEADER pSectionHead = (PIMAGE_SECTION_HEADER)((DWORD)&pNtHead->OptionalHeader + pNtHead->FileHeader.SizeOfOptionalHeader);

    DWORD dwPEHeadRellySize = (DWORD)pSectionHead + dwSectionHeadSize * pNtHead->FileHeader.NumberOfSections;
    dwPEHeadRellySize -= (DWORD)pDosHead;

    if ((pNtHead->OptionalHeader.SizeOfHeaders - dwPEHeadRellySize) < dwSectionHeadSize)
    {
        MessageBox(_T("剩余的空间不足以增加一个节"), _T("提示"), MB_OK);
        return;
    }

    InsertSection InsertSec;
    InsertSec.SetSectionType(ADD_SECTION);
    InsertSec.SetFilePath(m_szFileFullPath);
    InsertSec.SetFileData(&m_pFileData);
    InsertSec.SetOffset(dwPEHeadRellySize);
    InsertSec.DoModal();
}


//增加导入表项
void CMYPETOOLDlg::OnAddimport()
{

}
